﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;


namespace FCNS.Media
{
    /// <summary>  
    /// 提供全屏和指定窗口的截图 以及保存为文件的类  
    /// </summary>  
    public class Capture
    {
        /// <summary>  
        /// 全屏截图   
        /// </summary>  
        /// <returns></returns>  
        public static Image CaptureScreen()
        {
            return CaptureWindow(WinApi.GetDesktopWindow(),false);
        }
        /// <summary>  
        /// 指定窗口截图,可以截取非前端窗体的截图,但是非GDI的程序是无法截图的比如DirectX 
        /// </summary>  
        /// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>  
        /// <param name="gdi">是否 gdi 程序</param>
        /// <returns></returns>  
        public static System.Drawing.Image CaptureWindow(IntPtr handle,bool gdi)
        {
            if (gdi)
            {
                IntPtr hscrdc = WinApi.GetWindowDC(handle);
                WinApi.RECT windowRect = new WinApi.RECT();
                WinApi.GetWindowRect(handle, ref windowRect);
                int width = windowRect.Right - windowRect.Left;
                int height = windowRect.Bottom - windowRect.Top;

                IntPtr hbitmap = WinApi.CreateCompatibleBitmap(hscrdc, width, height);
                IntPtr hmemdc = WinApi.CreateCompatibleDC(hscrdc);
                WinApi.SelectObject(hmemdc, hbitmap);
                WinApi.PrintWindow(handle, hmemdc, 0);
                Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
                WinApi.DeleteDC(hscrdc);//删除用过的对象  
                WinApi.DeleteDC(hmemdc);//删除用过的对象  
                return bmp;
            }
            else
            {
                IntPtr hdcSrc = WinApi.GetWindowDC(handle);
                WinApi.RECT windowRect = new WinApi.RECT();
                WinApi.GetWindowRect(handle, ref windowRect);
                IntPtr hdcDest = WinApi.CreateCompatibleDC(hdcSrc);
                IntPtr hBitmap = WinApi.CreateCompatibleBitmap(hdcSrc, windowRect.Width, windowRect.Height);
                IntPtr hOld = WinApi.SelectObject(hdcDest, hBitmap);
                WinApi.BitBlt(hdcDest, 0, 0, windowRect.Width, windowRect.Height, hdcSrc, 0, 0, 0x00CC0020);
                WinApi.SelectObject(hdcDest, hOld);
                WinApi.DeleteDC(hdcDest);
                WinApi.ReleaseDC(handle, hdcSrc);
                Image img = Image.FromHbitmap(hBitmap);
                WinApi.DeleteObject(hBitmap);
                return img;
            }
        }
        /// <summary>
        /// 指定窗口截图,只要窗体的visable为true，即使它在屏幕的外面也可以抓到图。如果为false，就是一张黑图了。
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static Bitmap CaptureWindow2(IntPtr hWnd)
        {
            IntPtr hscrdc = WinApi.GetWindowDC(hWnd);
            IntPtr hbitmap = WinApi.CreateCompatibleBitmap(hscrdc, 800, 600);
            IntPtr hmemdc = WinApi.CreateCompatibleDC(hscrdc);
            WinApi.SelectObject(hmemdc, hbitmap);
            WinApi.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            WinApi.DeleteDC(hscrdc);//删除用过的对象  
            WinApi.DeleteDC(hmemdc);//删除用过的对象  
            return bmp;
        }
    }  
}
